package com.kd.juhaowan.ui.holder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.flyjingfish.shapeimageviewlib.ShapeImageView;

import com.kd.juhaowan.R;
import com.kd.juhaowan.http.model.SubCommentModel;
import com.kd.juhaowan.ui.adapter.BaseViewHolder;
import com.kd.juhaowan.ui.listener.OnItemClickListener;
import com.kd.juhaowan.ui.listener.OnItemDianzanListener;
import com.kd.juhaowan.ui.listener.OnItemPinglunListener;
import com.kd.juhaowan.utils.GlideHelper;
import com.kd.juhaowan.utils.LogUtil;
import com.kd.juhaowan.view.CommentImageView;
import com.luck.picture.lib.entity.LocalMedia;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class CommentDetailContentViewHolder extends BaseViewHolder {



    @BindView(R.id.item_avatar)
    ShapeImageView iconView;

    @BindView(R.id.item_name)
    TextView nameView;


    @BindView(R.id.item_level)
    TextView levelView;


    @BindView(R.id.item_dianzan)
    TextView dianzanView;
    @BindView(R.id.item_pinglun)
    TextView pinglunView;


    @BindView(R.id.item_content)
    TextView contentView;
    @BindView(R.id.item_images)
    RelativeLayout imageLayout;

    @BindView(R.id.item_diver)
    View diverView;


    @BindView(R.id.item_time)
    TextView timeView;
    @BindView(R.id.item_location)
    TextView addrView;



    private View.OnClickListener contentListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            OnItemClickListener listener = getOnItemClickListener();
            listener.onItemClick(CommentDetailContentViewHolder.this,contentView,getLayoutPosition());
        }
    };


    public CommentDetailContentViewHolder(@NonNull View itemView) {
        super(itemView);
    }

    @OnClick(R.id.item_dianzan)
    void onDianzanClick(){
        OnItemClickListener listener = getOnItemClickListener();
        if(listener instanceof OnItemDianzanListener){
            ((OnItemDianzanListener) listener).onItemDianzanClick(CommentDetailContentViewHolder.this,getLayoutPosition());
        }
    }
    @OnClick(R.id.item_pinglun)
    void onPinglunClick(){
        OnItemClickListener listener = getOnItemClickListener();
        if(listener instanceof OnItemPinglunListener){
            ((OnItemPinglunListener) listener).onItemPinglunClick(CommentDetailContentViewHolder.this,getLayoutPosition());
        }
    }

    private ArrayList<LocalMedia> mediaList=new ArrayList<>();

    @SuppressLint("ResourceType")
    private void setImageLayout(String[] images, boolean isChild, RelativeLayout iamgeLayout){
        if (images==null||images.length==0){

            iamgeLayout.setVisibility(View.GONE);

        }else if (images.length==1){
            if(!isChild){
                mediaList.add(LocalMedia.generateHttpAsLocalMedia(images[0]));

            }
            iamgeLayout.setVisibility(View.VISIBLE);
            iamgeLayout.removeAllViews();
            RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, UIUtil.dip2px(getContext(),isChild?140:178));
            lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            CommentImageView imageView = new CommentImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GlideHelper.imagePreview((Activity) getContext(),0,mediaList);
                }
            });

            GlideHelper.loadGoodsCover(Glide.with(getItemView()),imageView,images[0]);
            iamgeLayout.addView(imageView,lp1);
        }else if (images.length==2){
            for (String image:images) {
                if(!isChild){
                    mediaList.add(LocalMedia.generateHttpAsLocalMedia(image));
                }
            }
            iamgeLayout.setVisibility(View.VISIBLE);
            iamgeLayout.removeAllViews();
            int iconWidth = (UIUtil.getScreenWidth(getContext())-UIUtil.dip2px(getContext(),isChild?70:50)-UIUtil.dip2px(getContext(),isChild?8:10))/2;
            RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                    iconWidth, UIUtil.dip2px(getContext(),110));
            lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);


//            RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
//                    RelativeLayout.LayoutParams.MATCH_PARENT, UIUtil.dip2px(getContext(),isChild?140:178));
//            lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            CommentImageView imageView = new CommentImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GlideHelper.imagePreview((Activity) getContext(),0,mediaList);
                }
            });

            GlideHelper.loadGoodsCover(Glide.with(getItemView()),imageView,images[0]);
            iamgeLayout.addView(imageView,lp1);

            CommentImageView imageView2 = new CommentImageView(getContext());
            imageView2.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GlideHelper.imagePreview((Activity) getContext(),1,mediaList);
                }
            });
            GlideHelper.loadGoodsCover(Glide.with(getItemView()),imageView2,images[1]);

            RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
                    iconWidth, UIUtil.dip2px(getContext(),110));
            lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            iamgeLayout.addView(imageView2,lp2);
        }

        else if(images.length>2){
            for (String image:images) {
                if(!isChild){
                    mediaList.add(LocalMedia.generateHttpAsLocalMedia(image));
                }
            }
            iamgeLayout.setVisibility(View.VISIBLE);
            iamgeLayout.removeAllViews();
            int iconWidth = (UIUtil.getScreenWidth(getContext())-UIUtil.dip2px(getContext(),isChild?70:50)-UIUtil.dip2px(getContext(),isChild?8:10))/3;
            RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                    iconWidth, iconWidth);
            lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            CommentImageView imageView = new CommentImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GlideHelper.imagePreview((Activity) getContext(),0,mediaList);
                }
            });
            GlideHelper.loadGoodsCover(Glide.with(getItemView()),imageView,images[0]);
            CommentImageView imageView2 = new CommentImageView(getContext());
            imageView2.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GlideHelper.imagePreview((Activity) getContext(),1,mediaList);
                }
            });
            GlideHelper.loadGoodsCover(Glide.with(getItemView()),imageView2,images[1]);
            iamgeLayout.addView(imageView,lp1);
            RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
                    iconWidth, iconWidth);
            lp2.addRule(RelativeLayout.CENTER_IN_PARENT);


            iamgeLayout.addView(imageView2,lp2);
            if(images.length>=3){

                CommentImageView imageView3 = new CommentImageView(getContext());
                imageView3.setId(isChild?222222:11111);
                imageView3.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        GlideHelper.imagePreview((Activity) getContext(),2,mediaList);
                    }
                });
                GlideHelper.loadGoodsCover(Glide.with(getItemView()),imageView3,images[2]);
                RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
                        iconWidth, iconWidth);
                lp3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                iamgeLayout.addView(imageView3,lp3);
                if(images.length>3){
                    RelativeLayout.LayoutParams lptv = new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    lptv.addRule(RelativeLayout.ALIGN_BOTTOM,11111);
                    lptv.addRule(RelativeLayout.ALIGN_RIGHT,11111);
                    TextView tv =new TextView(getContext());
                    tv.setText("+"+(images.length-3));
                    tv.setPadding(8,4,8,4);
                    tv.setTextSize(10);
                    tv.setTextColor(getContext().getColor(R.color.white));
                    tv.setBackgroundResource(R.drawable.comment_image_more);
                    iamgeLayout.addView(tv,lptv);
                }
            }
        }
    }

    private SpannableString getSpanString(SubCommentModel model){
        String temp ="";
//         if(model.getIs_author()==1){
//             temp+=" 楼主 ";
//         }
        if(model.getUser_reply_text()!=null&&!TextUtils.isEmpty(model.getUser_reply_text().getNickname())){
            temp+="回复@"+model.getUser_reply_text().getNickname()+": ";
        }
        temp+=model.getContent();
        SpannableString spannableString = new SpannableString(temp);
//        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#603FE0")), temp.indexOf(model.getUser_text().getNickname()), temp.indexOf(model.getUser_text().getNickname())+model.getUser_text().getNickname().length(), 0);
//        if(model.getIs_author()==1){
//            temp+=" 楼主 ";
//            spannableString.setSpan(new AbsoluteSizeSpan(10,true), temp.indexOf("楼主"), temp.indexOf("楼主")+2, 0);
//            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#898A8F")), temp.indexOf("楼主"), temp.indexOf("楼主")+2, 0);
//        }

        if(model.getUser_reply_text()!=null&&!TextUtils.isEmpty(model.getUser_reply_text().getNickname())){
            LogUtil.debugLog("name"+model.getUser_reply_text().getNickname());
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#603FE0")), temp.indexOf("回复")+2, temp.lastIndexOf(model.getUser_reply_text().getNickname())+model.getUser_reply_text().getNickname().length(), 0);
        }
        LogUtil.debugLog(model.toString()+"\n"+spannableString);
        return spannableString;
    }
    @SuppressLint("ResourceType")
    public void setDate(SubCommentModel model,boolean isLast){
//        bankName.setText(model.getName());
        diverView.setVisibility(isLast?View.GONE:View.VISIBLE);

        GlideHelper.loadGoodsCover(Glide.with(getItemView()),iconView,model.getUser_text().getAvatar());
        nameView.setText(model.getUser_text().getNickname());
        levelView.setText("Lv"+model.getUser_text().getLevel());

        contentView.setText(getSpanString(model));
//        contentView.setMovementMethod(LinkMovementMethod.getInstance());
//        contentView.setClickable(true);
//        contentView.setOnClickListener(contentListener);
//        contentView.setText(AtUserHelper.parseAtUserLink(model.getContent(),getContext()));
        dianzanView.setText(model.getLike_count()+"");
//        pinglunView.setText(model.getNumber_replies()+"");

        timeView.setText(model.getCreatetime().contains(" ")?model.getCreatetime().substring(0,model.getCreatetime().indexOf(" ")):model.getCreatetime());

//        addrView.setText(model.getPlaceof_posting());

        dianzanView.setCompoundDrawablePadding(UIUtil.dip2px(getContext(),5));
        if(model.getIs_like()==1){
            dianzanView.setCompoundDrawablesWithIntrinsicBounds(getContext().getDrawable(R.mipmap.icon_dianzan_self),null,null,null);
        }else {
            dianzanView.setCompoundDrawablesWithIntrinsicBounds(getContext().getDrawable(R.mipmap.icon_dianzan_default),null,null,null);
        }
        imageLayout.removeAllViews();
        mediaList.clear();

            String[] images= model.getImages();
            setImageLayout(images,false,imageLayout);
            LogUtil.debugLog((images==null)+"");


        }
    }



